const Fields = require("../model/fields.model.js");
class FieldsService {
  //保存接头盒信息，如果没有companyId则为新增，否则为更新
  async saveFields({
    fieldId,
    fieldName,
    fieldType,
    fieldModel,
    dataFlag,
  }) {
    var update = false;
    var fields = {};
    var whereOpt = {};
    if (ID && ID > 0) {
      update = true;
      whereOpt.ID = ID;
    }
    fieldModel && Object.assign(fields, { fieldModel });
    fieldId >= 0 && Object.assign(fields, { fieldId });
    fieldName && Object.assign(fields, { fieldName });
    fieldType && Object.assign(fields, { fieldType });
    dataFlag >= 0 && Object.assign(fields, { dataFlag });
    if (Object.keys(fields).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Fields.update(fields, { where: whereOpt });
        if (res[0] > 0) {
          fields.fieldId = fieldId;
          return JSON.parse(JSON.stringify(fields));;
        } else {
          return false;
        }
      } else {
        res = await Fields.create(fields);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return JSON.parse(JSON.stringify(res.dataValues));
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除fields
  async eraseFieldsById(fieldId) {
    if (!fieldId || fieldId <= 0) {
      return false;
    }
    var res;
    try {
      res = Fields.destroy({ where: { fieldId } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
    /* 
  删除指定条件的接头盒
  */
 async eraseFields({fieldsId}){
  var whereOpt = {};
  fieldsId && Object.assign(whereOpt,{fieldsId});
  if(Object.keys(whereOpt).length == 0){
    return false;
  }
  var res;
  try {
    res = await Fields.destroy({where:whereOpt});
    if(!res){
      return false;
    }else{
      return true;
    }
  } catch (error) {
    console.log(error);
    return null;
  }
 }
   /* 
  批量添加
  */
  async  batchAddFields(arr){
    if(!(arr instanceof Array) || arr.length == 0){
      return false;
    }
    var res;
    try {
      res = await Fields.bulkCreate(arr,{
        fields:[
          "fieldId",
          "fieldName",
          "fieldType",
          "fieldModel"
        ]
      });
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /*
   *
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getFields({ fieldId, fieldName, fieldType,fieldModel }) {
    var whereOpt = {};
    fieldId >= 0 && Object.assign(whereOpt, { fieldId });
    fieldName && Object.assign(whereOpt, { fieldName });
    fieldType && Object.assign(whereOpt, { fieldType });
    fieldModel && Object.assign(whereOpt, { fieldModel });
    var res;
    try {
      res = await Fields.findAll({
        where: whereOpt,
        order: [["fieldId", "ASC"]],
      });
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = new FieldsService();
